import Vue from "vue";
import VueRouter from "vue-router";
// import index from '../src/components/index.vue'
import { Notification, MessageBox, Message } from 'element-ui'
import draw from '../src/components/draw.vue'
import integral from '../src/components/integral.vue'
import turntable from '../src/components/turntable.vue'
import ActivityCenter from '../src/components/ActivityCenter.vue'
import Prizestatus from '../src/components/Prizestatus.vue'
import Participate from '../src/components/Participate.vue'
import request from "@/utils/request";
import banShare from "@/utils/banShare";
import { server } from "@/utils/env";
import wx from "weixin-js-sdk";
import {
    getCode,
    getToken,
    registerDay
} from "@/utils/auth";
import {
    resolve
} from "core-js/fn/promise";
// import Questionnaire from "../src/components/Questionnaire.vue";


Vue.use(VueRouter);
const routes = [{
        path: "/",
        component: () =>
            import ('../src/components/index.vue'),
        name: "index"
    },
    {
        path: "/draw",
        component: draw,
        name: "draw"
    },
    {
        path: "/integral",
        component: integral,
        name: "integral"
    },
    {
        path: "/turntable",
        component: turntable,
        name: "turntable"
    },
    // {
    // 	path: "/index",
    // 	component: index,
    // 	name: "index"
    // },
    {
        path: "/ActivityCenter",
        component: ActivityCenter,
        name: "ActivityCenter"
    },
    {
        path: "/Prizestatus",
        component: Prizestatus,
        name: "Prizestatus"
    },
    // {
    // 	path: "/Questionnaire",
    // 	component: Questionnaire,
    // 	name: "Questionnaire"
    // },
    {
        path: "/Participate",
        component: Participate,
        name: "Participate"
    },

    //系统消息
    {
        path: "/redirect",
        component: () =>
            import ('../src/components/redirect.vue'),
        name: "redirect"
    },
    //网络等待
    {
        path: "/laoding",
        component: () =>
            import ('../src/components/page/laoding/laoding.vue'),
        name: "laoding"
    },
    // wzx
    //系统消息
    {
        path: "/SystemMessages",
        component: () =>
            import ('../src/components/page/systemMessages/systemMessages.vue'),
        name: "SystemMessages"
    },
    //感谢参与
    {
        path: "/Thanks",
        component: () =>
            import ('../src/components/page/thanks/thanks.vue'),
        name: "Thanks"
    },
    //个人信息完善
    {
        path: "/personalInformation",
        component: () =>
            import ('../src/components/page/personalInformation/personalInformation.vue'),
        name: "personalInformation"
    },
    //积分明细
    {
        path: "/IntegralRecord",
        component: () =>
            import ('../src/components/page/IntegralRecord/IntegralRecord.vue'),
        name: "IntegralRecord"
    },
    //打卡
    {
        path: "/clockIn",
        component: () =>
            import ('../src/components/page/clockIn/clockIn.vue'),
        name: "clockIn"
    },
    {
        path: "/ceshi",
        component: () =>
            import ('../src/components/page/ceshi/ceshi.vue'),
        name: "ceshi"
    },
    {
        path: "/votelist",
        component: () =>
            import ('../src/components/page/votelist/votelist.vue'),
        name: "votelist"
    },
    {
        path: "/thanksuser",
        component: () =>
            import ('../src/components/page/thanksuser/thanksuser.vue'),
        name: "thanksuser"
    },
    {
        path: "/inpackeg",
        component: () =>
            import ('../src/components/page/inpackeg/inpackeg.vue'),
        name: "inpackeg"
    },
    {
        path: "/cearchPage",
        component: () =>
            import ('../src/components/page/cearchPage/cearchPage.vue'),
        name: "cearchPage"
    },
    {
        path: "/onlinerevews",
        component: () =>
            import ('../src/components/page/OnlineReviews/OnlineReviews.vue'),
        name: "onlinerevews"
    },
    {
        path: "/reviewDetail",
        component: () =>
            import ('../src/components/page/reviewDetail/reviewDetail.vue'),
        name: "reviewDetail"
    },
    {
        path: "/readPdf",
        component: () =>
            import ('../src/components/page/readPdf/readPdf.vue'),
        name: "readPdf"
    },
    // 抽奖列表
    {
        path: "/luckDraw",
        component: () =>
            import ('../src/components/page/luckDraw/luckDraw.vue'),
        name: "luckDraw"
    },
    // 有奖答题
    {
        path: "/answerList",
        component: () =>
            import ('../src/components/page/answerList/answerList.vue'),
        name: "answerList"
    },
    {
        path: "/answerDetail",
        component: () =>
            import ('../src/components/page/answerDetail/answerDetail.vue'),
        name: "answerDetail"
    },
    // 答题积分详细
    {
        path: "/recordDetail",
        component: () =>
            import ('../src/components/page/recordDetail/recordDetail.vue'),
        name: "recordDetail"
    },
    {
        path: "/jumpGZH",
        component: () =>
            import ('../src/components/page/jumpGZH/jumpGZH.vue'),
        name: "jumpGZH"
    },
    {
        path: "/DetailsQuestionnaire",
        component: () =>
            import ('../src/components/page/DetailsQuestionnaire/DetailsQuestionnaire.vue'),
        name: "DetailsQuestionnaire"
    },
    {
        path: "/ShareGifts",
        component: () =>
            import ('../src/components/page/ShareGifts/ShareGifts.vue'),
        name: "ShareGifts",

    },
    // mate: {
    // 	checkMethodName: "getShareGifts",
    // 	istoken: true
    // }
    {
        path: "/attention",
        component: () =>
            import ('../src/components/page/attention/attention.vue'),
        name: "attention"
    },
    {
        path: "/detailPage",
        component: () =>
            import ('../src/components/page/detailPage/detailPage.vue'),
        name: "detailPage",
        mate: {
            checkMethodName: "getShareInfo",
            istoken: true
        }
    },
];


const router = new VueRouter({
    mode: 'history',
    routes
});


const pathUrl = location.href.split('#')[0]
    // const pathUrl=server.domain
    //阅读文章
const getShareInfo = async({
    query,
    name
}) => {
    let userId
    let shareInfo = {
        detailPage: {
            title: '将文章分享给你的好友！',
            url: pathUrl,
            way: 7
        },
        answerDetail: {
            title: '这题太难了快来帮帮我吧！',
            url: pathUrl,
            way: 15
        },
        clockIn: {
            title: '这个活动很哇塞来和我一起参加吧！',
            url: pathUrl,
            way: 5
        },
        DetailsQuestionnaire: {
            title: '问卷分享给你喽~来填一填吧！',
            url: pathUrl,
            way: 11
        },
        Participate: {
            title: '投票开始了，开始你的选择吧！',
            url: pathUrl,
            way: 13
        },
        ShareGifts: {
            title: '分享给朋友,获得积分奖励！'
        },
    }
    return new Promise(async(resolve, reject) => {
        await request({
            url: "/generator/user/front/getinfo",
            method: "get",
        }).then((response) => {
            userId = response.user.id;
            localStorage.setItem('user', response.user.id)
        });
        let link = `${pathUrl}&shareUserId=${userId}`
        await request({
                url: "generator/user/front/sign",
                method: "post",
                data: {
                    // url:`${server.domain}-${encodeURIComponent(link)}`
                    // url:shareInfo[name].url
                    url: pathUrl
                },
            })
            //getShare(data) //这里我写了一个公用的接口请求js，这里正常axios请求就可以，只要拿到数据都可以
            .then((res) => {
                localStorage.setItem("jsapi_ticket", res.jsapi_ticket);
                //拿到后端给的这些数据
                // let appId = res.appId;
                console.log(query.row);
                let timestamp = res.timestamp;
                let nonceStr = res.nocestr;
                let signature = res.signature;
                let shareUserId = userId;
                console.log(name, shareInfo[name].title)
                let link = `${pathUrl}&shareUserId=${shareUserId}`
                let config = {
                    title: shareInfo[name].title,
                    desc: "获得积分",
                    link: res.url,
                    imgUrl: "https://demoee.oss-cn-shanghai.aliyuncs.com/point/1637999742809.jpg",
                    type: "",
                    dataUrl: "",
                    success: () => {
                        if (shareInfo[name].way) {
                            request({
                                url: "/generator/user/front/share?way=" + shareInfo[name].way + "&shareuserId=" + userId,
                                method: "get",
                            }).then((response) => {});
                        }
                        console.log("分享成功!");
                    },
                    cancel: function() {
                        console.log("取消分享!");
                    },
                };
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: server.appId, // 必填，公众号的唯一标识，填自己的！
                    timestamp: timestamp, // 必填，生成签名的时间戳，刚才接口拿到的数据
                    nonceStr: nonceStr, // 必填，生成签名的随机串
                    signature: signature, // 必填，签名，见附录1
                    jsApiList: [
                        "updateTimelineShareData",
                        "updateAppMessageShareData",
                        "onMenuShareAppMessage",
                    ],
                });

                wx.ready(function() {
                    console.log(' wx.config成功', res);
                    //分享给朋友
                    wx.onMenuShareAppMessage(config);
                    // wx.updateAppMessageShareData(config);
                });
                wx.error(function(res) {
                    console.log(' wx.config失败', res);
                });
            })
        resolve()
    })
}
const getShareGifts = async() => {
    let userId
    let link = server.domain + 'attention?shareUserId=' + userId
    return new Promise(async(resolve, reject) => {
        await request({
            url: "/generator/user/front/getinfo",
            method: "get",
        }).then((response) => {
            userId = response.user.id;
            localStorage.setItem('user', response.user.id)
        });
        await request({
            url: "generator/user/front/sign",
            method: "post",
            data: {
                url: pathUrl
            },
        }).then((res) => {
            localStorage.setItem("jsapi_ticket", res.jsapi_ticket);
            let timestamp = res.timestamp;
            let nonceStr = res.nocestr;
            let signature = res.signature;
            let shareUserId = userId;
            let config = {
                title: "分享给朋友,获得积分奖励1",
                desc: "获得积分",
                link: `${pathUrl}?shareUserId=${shareUserId}`,
                imgUrl: "",
                type: "",
                dataUrl: "",
                success: function() {
                    console.log("分享成功!");
                },
                cancel: function() {
                    console.log("取消分享!");
                },
            };
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: server.appId, // 必填，公众号的唯一标识，填自己的！
                timestamp: timestamp, // 必填，生成签名的时间戳，刚才接口拿到的数据
                nonceStr: nonceStr, // 必填，生成签名的随机串
                signature: signature, // 必填，签名，见附录1
                jsApiList: ["onMenuShareAppMessage", "updateAppMessageShareData", "updateTimelineShareData"],
            });

            wx.ready(function() {
                //分享到朋友圈

                //分享给朋友
                // wx.onMenuShareAppMessage(config);
                wx.updateAppMessageShareData(config);
            });

            wx.error(function(res) {
                console.log(res);
            });
        })
        resolve()
    })
}
const banSharePage = () => {
    request({
        url: "generator/user/front/sign",
        method: "post",
        data: {
            url: pathUrl
        },
    }).then((res) => {
        banShare(res)
    })
}
const checkMethods = {
    detailPage: getShareInfo,
    answerDetail: getShareInfo,
    clockIn: getShareInfo,
    DetailsQuestionnaire: getShareInfo,
    Participate: getShareInfo,
    ShareGifts: getShareInfo

}
const checkMetShare = {
    ShareGifts: getShareGifts
}
const checkBanShare = {
    index: banSharePage,
    integral: banSharePage,
    personalInformation: banSharePage,
    luckDraw: banSharePage,
    ActivityCenter: banSharePage,
    cearchPage: banSharePage,
    votelist: banSharePage,
    onlinerevews: banSharePage,
    answerList: banSharePage,
}
router.beforeEach(async(to, from, next) => {
    // await getCode()
    // if (getToken()) {
    //     await registerDay()

        if (checkMethods[to.name]) {
            await checkMethods[to.name](to)
        } else if (checkBanShare[to.name]) {
            await checkBanShare[to.name]()
        }
        // else if (checkMetShare[to.name]) {
        // 	await checkMetShare[to.name](to)
        // }
        next();
    // }
});

export default router;